package vu.alfonsodiecko.stundenplanfhjena.gui;

import org.holoeverywhere.ArrayAdapter;
import org.holoeverywhere.widget.AdapterView;
import org.holoeverywhere.widget.AdapterView.OnItemSelectedListener;
import org.holoeverywhere.widget.Button;
import org.holoeverywhere.widget.Spinner;

import vu.alfonsodiecko.stundenplanfhjena.Project;
import vu.alfonsodiecko.stundenplanfhjena.R;
import vu.alfonsodiecko.stundenplanfhjena.SQLiteHelper;
import vu.alfonsodiecko.stundenplanfhjena.SharedPreferencesHelper;
import vu.alfonsodiecko.stundenplanfhjena.datentyp.raum.Raeume;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class FragmentRaumtLoader extends SherlockFragment implements OnItemSelectedListener {
	private Spinner spinnerRaum;

	private Raeume raeume;

	private Context con;
	private int selection1;

	private SQLiteHelper db;

	private View v;

	public void onAttach(Activity activity) {
		super.onAttach(activity);
		con = activity.getApplicationContext();
		db = new SQLiteHelper(con);
		raeume = db.getRaume();
		db.close();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (raeume.getRaumListeText().size() == 0) {
			v = inflater.inflate(R.layout.error, container, false);

		} else {
			v = inflater.inflate(R.layout.raum, container, false);
			Button button = (Button) v.findViewById(R.id.buttonSave);
			button.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					saveSettings(v);
				}
			});
			spinnerRaum = (Spinner) v.findViewById(R.id.spinnerRaum);
			ArrayAdapter<String> adapterspinnerRaum = new ArrayAdapter<String>(con, R.layout.spinner_view,
					raeume.getRaumListeText());
			spinnerRaum.setAdapter(adapterspinnerRaum);
			spinnerRaum.setOnItemSelectedListener(this);
			SharedPreferencesHelper spt = new SharedPreferencesHelper(con);
			String val1 = spt.getSharedPreferences(Project.Strings.RaumSet);
			if (!val1.equals("")) {
				spinnerRaum.setSelection(getSelectionValue(spinnerRaum, val1));

			}
		}

		return v;
	}

	private int getSelectionValue(Spinner spinner, String diff) {
		int position = 0;
		String[] values = getValuesBySpinner(spinner.getAdapter());
		if (values != null) {
			for (String str : values) {
				if (str.equals(diff)) {
					return position;
				}
				position++;
			}
		}
		return 0;
	}

	private String[] getValuesBySpinner(SpinnerAdapter adapter) {
		if (adapter != null) {
			int size = adapter.getCount();
			String[] values = new String[size];
			int i = 0;
			while (i < size) {
				values[i] = (String) adapter.getItem(i);
				i++;
			}
			return values;
		}
		return null;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (arg0.getId()) {
		case R.id.spinnerRaum:
			selection1 = arg2;

			break;
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	public void saveSettings(View view) {
		SharedPreferencesHelper spt = new SharedPreferencesHelper(con);
		String dbID = String.valueOf(raeume.get(selection1).getId());
		spt.setSharedPreferences(Project.Strings.dbId, dbID);
		spt.setSharedPreferences(Project.Strings.dbChange, "true");
		spt.setSharedPreferences(Project.Strings.dbIDMode, "raum");
		spt.setSharedPreferences(Project.Strings.RaumSet, raeume.get(selection1).getName());
		Toast.makeText(con, "Settings Saved:\nRaum: " + raeume.get(selection1).getName(), Toast.LENGTH_LONG).show();
		spt = new SharedPreferencesHelper(con);
		spt.setSharedPreferences(Project.Strings.install, "done");
		this.startActivity(new Intent(con, TerminPlaner.class));
	}

}
